# Copyright (c) 2009 Barry Schwartz
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

#--------------------------------------------------------------------------

import glob
from os.path import abspath, basename, curdir, join
from subprocess import Popen, PIPE
from site_utilities import *

Import('*')

env = env.Clone()

#--------------------------------------------------------------------------

hyph_m4_files = glob.glob(join(hyph_dir, '*.hyphen.m4'))

if hyph_langs == None:
    hyph_files = glob.glob(join(hyph_dir, '*.hyphen'))
    hyph_files = [str(h) for h in hyph_files]
    for m4_file in hyph_m4_files:
        if str(m4_file)[:-3] not in hyph_files:
            hyph_files.append(str(m4_file)[:-3])
else:
    hyph_files = [join(hyph_dir, s + '.hyphen')
                  for s in hyph_langs]

M4(hyph_m4_files)
NoClean(hyph_files)

#--------------------------------------------------------------------------

def HyphenTable_builder(source, target, env):

    program_to_run = str(source[0])
    args = [str(pattern_file) for pattern_file in source[1:]]

    generated_ml_file  = str(target[0])

    f = open(generated_ml_file, 'w')
    program = Popen([program_to_run] + args, stdout = PIPE)
    for line in program.stdout:
        f.write(line)
    f.close()

    return 0

env['BUILDERS']['HyphenTable'] = Builder(action = HyphenTable_builder)

#--------------------------------------------------------------------------

env['OCAMLINCLUDES'].append(subdirs['Runtime'])
env['OCAMLINCLUDES'].append(subdirs['Trie'])
env['OCAMLINCLUDES'].append(subdirs['Unicode'])
env['OCAMLINCLUDES'].append(abspath(curdir))

plugin_list = []

for hyph in hyph_files:
    env1 = env.Clone()

    plugin_name = basename(hyph)[:-7] + '_hyphen_' + arithmetic_module.lower()
    plugin_target  = join(plugin_dir, plugin_name)

    env1.HyphenTable([plugin_name + '_table.include'], [gen_hyph_table, hyph])
    env1.Depends([plugin_name + '_table.include'], [gen_hyph_table, hyph])
    
    env1['M4FLAGS'] = '-E -DINCLUDE_FILE=' + plugin_name + '_table.include'
    env1.M4([plugin_name + '.ml'], ['hyph-template.m4'])
    env1.Depends(plugin_name + '.ml', plugin_name + '_table.include')

    obj = env1.OCamlObject(plugin_name)
    plug = env1.OCamlPlugin(plugin_target, obj)
    plugin_list = plugin_list + plug

    # Something is buggy in our ocaml.py; this is a workaround.
    Depends([abspath(str(p)) for p in plug],
            [abspath(str(o)) for o in obj])


Return('plugin_list')

#--------------------------------------------------------------------------

# local variables:
# mode: python
# python-indent: 4
# end:
